local ThinkORM = require "tianku/core/thinkorm"

print("\n=== ThinkORM Test ===")
print("Version:", ThinkORM._VERSION)
print("Lua Version:", _VERSION)
 

-- 初始化测试数据库
local function init_test_db()
    -- 删除旧测试数据库
    os.execute("rm -f /tmp/thinkorm_test.db")
    os.execute("rm -f /tmp/thinkorm_test2.db")
    
    -- 创建表结构
    ThinkORM.execute([[
        CREATE TABLE think_users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            email TEXT UNIQUE,
            age INTEGER,
            status INTEGER DEFAULT 1,
            created_at INTEGER
        )
    ]])
    
    ThinkORM.execute([[
        CREATE TABLE think_orders (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER,
            amount REAL,
            status INTEGER DEFAULT 0,
            created_at INTEGER,
            FOREIGN KEY(user_id) REFERENCES think_users(id)
        )
    ]], nil, "secondary")
end

-- 测试基本CRUD
local function test_basic_crud()
    print("\n=== 测试基本CRUD ===")
    
    -- 使用 name() 方法自动添加前缀
    local User = ThinkORM.name("users")
    
    -- 插入数据
    local user_id = User:data({
        name = "张三",
        email = "zhangsan@example.com",
        age = 25,
        created_at = os.time()
    }):insert()
    
    print("插入用户ID:", user_id)
    
    -- 查询数据
    local user = User:where({id = user_id}):find()
    print("查询用户:", user.name, user.email, user.age)
    
    -- 更新数据
    local affected = User:where({id = user_id}):data({
        age = 26,
        status = 2
    }):update()
    print("更新影响行数:", affected)
    
    -- 验证更新
    user = User:where({id = user_id}):find()
    print("更新后年龄:", user.age)
    
    -- 删除数据
    affected = User:where({id = user_id}):delete()
    print("删除影响行数:", affected)
    
    -- 验证删除
    user = User:where({id = user_id}):find()
    print("删除后查询:", user == nil and "nil" or "存在")
end

-- 测试基本类似 Db::table()
local function test_dbtab_crud()
 

  print("\n=== 测试基本CRUD 带前缀 ===")
    
    -- 插入数据 需要指定完整表名(带前缀)
-- 使用 name() 方法自动添加前缀
    local User = ThinkORM.table('think_users')
    
    -- 插入数据
    local user_id = User:data({
        name = "张三",
        email = "zhangsan@example.com",
        age = 25,
        created_at = os.time()
    }):insert()
    
    print("插入用户ID:", user_id)
    -- 需要指定完整表名(带前缀)
local user = ThinkORM.table('think_users')
    :where('id', user_id)
    :find()

print(user.name, user_id)

    -- 查询数据
    local user = User:where({id = user_id}):find()
    print("查询用户:", user.name, user.email, user.age)
    
    -- 更新数据
    local affected = User:where({id = user_id}):data({
        age = 26,
        status = 2
    }):update()
    print("更新影响行数:", affected)
    
    -- 验证更新
    user = User:where({id = user_id}):find()
    print("更新后年龄:", user.age)
    
    -- 删除数据
    affected = User:where({id = user_id}):delete()
    print("删除影响行数:", affected)
    
    -- 验证删除
    user = User:where({id = user_id}):find()
    print("删除后查询:", user == nil and "nil" or "存在")

end


-- 测试事务
local function test_transaction()
    print("\n=== 测试事务 ===")
    
    local User = ThinkORM.name("users")
    local Order = ThinkORM.name("orders")
    
    -- 创建测试用户
    local user_id = User:data({
        name = "李四",
        email = "lisi@example.com",
        age = 30,
        created_at = os.time()
    }):insert()
    
    -- 开始事务
    ThinkORM.startTrans()
    
    local ok, err = pcall(function()
        -- 扣减年龄
        User:where({id = user_id}):decr("age", 5):update()
        
        -- 创建订单
        local order_id = Order:data({
            user_id = user_id,
            amount = 100.50,
            created_at = os.time()
        }):insert()
        
        print("事务内订单ID:", order_id)
        
        -- 故意制造一个错误
        -- local a = nil + 1
        
        ThinkORM.commit()
        print("事务提交成功")
    end)
    
    if not ok then
        ThinkORM.rollback()
        print("事务回滚:", err)
    end
    
    -- 验证结果
    local user = User:where({id = user_id}):find()
    local orders = Order:where({user_id = user_id}):select()
    
    print("用户年龄:", user.age)
    print("订单数量:", #orders)
end

-- 测试多数据库连接
local function test_multi_connection()
    print("\n=== 测试多数据库连接 ===")
    
    -- 默认连接
    local User = ThinkORM.name("users")
    
    -- 第二个数据库连接
    local Order = ThinkORM.table("think_orders", "secondary")
    
    -- 插入用户
    local user_id = User:data({
        name = "王五",
        email = "wangwu@example.com",
        age = 35,
        created_at = os.time()
    }):insert()
    
    -- 插入订单(到第二个数据库)
    local order_id = Order:data({
        user_id = user_id,
        amount = 200.75,
        created_at = os.time()
    }):insert()
    
    print("用户ID:", user_id)
    print("订单ID:", order_id)
    
    -- 查询验证
    local user = User:where({id = user_id}):find()
    local order = Order:where({id = order_id}):find()
    
    print("用户名:", user.name)
    print("订单金额:", order.amount)
end

-- 运行测试
print("=== ThinkORM Lua 测试 ===")
print("版本:", ThinkORM._VERSION)

init_test_db()
test_basic_crud()
test_dbtab_crud()
test_transaction()
test_multi_connection()

print("\n=== 所有测试完成 ===")